﻿using Microsoft.Extensions.Configuration;
using System.IO;

namespace Koala.Pro.Common
{
    public class ConfigHelper
    {
        public static string GetSectionValue(string key)
        {
            string text = "appsettings.json";
            string baseDirectory = AppContext.BaseDirectory;
            baseDirectory = baseDirectory.Replace("\\", "/");
            string path = baseDirectory + text;
            if (!File.Exists(path))
            {
                path = baseDirectory[..baseDirectory.IndexOf("/bin")] + "/" + text;
            }
            return new ConfigurationBuilder().AddJsonFile(path, optional: false, reloadOnChange: true).Build().GetSection(key)
                .Value;
        }

        public static DbSetting GetDbSettings(string dbConfigName = null)
        {
            DbSetting dbSetting = new DbSetting();
            dbSetting.DbType = GetSectionValue("DbSetting:ComponentDbType") ?? "sqlserver";
            dbSetting.DbConfigName = dbConfigName ?? GetSectionValue("DbSetting:DefaultDb");
            string key = $"ConnectionStrings:{dbSetting.DbConfigName}";
            dbSetting.ConnectionString = GetSectionValue(key);
            if (!string.IsNullOrEmpty(dbConfigName))
            {
                dbSetting.DbType = GetDbType(dbConfigName);
            }
            return dbSetting;
        }

        public static string GetConnectionString(string dbConfigName)
        {
            return GetSectionValue($"ConnectionStrings:{dbConfigName}");
        }

        private static string GetDbTypeFromProvider(string providerName)
        {
            string result = "SqlServer";
            if (!string.IsNullOrEmpty(providerName))
            {
                if (providerName.EndsWith("MySqlClient", StringComparison.OrdinalIgnoreCase))
                {
                    result = "MySql";
                }
                else if (providerName.EndsWith("SqlClient", StringComparison.OrdinalIgnoreCase))
                {
                    result = "SqlServer";
                }
                else if (providerName.EndsWith("SQLite", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Sqlite";
                }
                else if (providerName.IndexOf("Oracle", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    result = "Oracle";
                }
                else if (providerName.EndsWith("Npgsql", StringComparison.OrdinalIgnoreCase))
                {
                    result = "PostgreSQL";
                }
                else if (providerName.Equals("Dm", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Dm";
                }
            }
            return result;
        }

        private static string GetDbType(string dbConfigName)
        {
            string result = "SqlServer";
            if (!string.IsNullOrEmpty(dbConfigName))
            {
                if (dbConfigName.IndexOf("mysql", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    result = "MySql";
                }
                else if (dbConfigName.IndexOf("sqlite", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    result = "Sqlite";
                }
                else if (dbConfigName.IndexOf("npgsql", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    result = "PostgreSQL";
                }
                else if (dbConfigName.IndexOf("sqlserver", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    result = "SqlServer";
                }
                else if (dbConfigName.IndexOf("oracle", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    result = "Oracle";
                }
                else if (dbConfigName.Equals("Dm", StringComparison.OrdinalIgnoreCase))
                {
                    result = "Dm";
                }
            }
            return result;
        }
    }

}
